package com.tengju.bff.interfaces.manage.crm.activity;

import com.tengju.bff.application.shared.ApplicationException;
import com.tengju.bff.interfaces.manage.crm.activity.facade.ActivityManageFacade;
import com.tengju.bff.interfaces.manage.crm.activity.form.ActivityListForm;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.motivation.application.activity.ActivityApplicationService;
import com.tengju.motivation.domain.model.activity.ActivityId;
import com.tengju.support.domain.annotation.BizLog;
import com.tengju.support.domain.annotation.BizLogSysName;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.tengju.user.domain.shared.ResultCodeEnum.BIZ_ERROR;
import static com.tengju.user.domain.shared.ResultCodeEnum.SYS_EXCEPTION;

@Validated
@RestController
@Api(tags = "组织管理-销售活动管理")
@RequestMapping(value = "manage/crm/activity/")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ActivityManageController {

    private final ActivityManageFacade activityFacade;
    private final ActivityApplicationService activityApplicationService;

    @ApiOperation(value = "活动名称列表", notes = "活动名称列表")
    @GetMapping(value = "/name/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO<String, Long>>> nameList() {
        return ApiResponse.newSuccess(activityFacade.nameList());
    }

    @ApiOperation(value = "活动列表", notes = "活动列表")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> list(ActivityListForm form) {
        return ApiResponse.newSuccess(activityFacade.list(form));
    }


    @BizLog(sysName = BizLogSysName.TENGJU_ACTIVITY_TARGET_MANAGE)
    @ApiOperation(value = "活动删除", notes = "活动删除")
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<Void> delete(Long activityId) {
        try {
            activityApplicationService.delete(new ActivityId(activityId));
            return ApiResponse.newSuccess();
        } catch (ApplicationException e) {
            return ApiResponse.newFailure(BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("删除失败", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }


}
